package com.lill.sale.util;

import java.math.BigInteger;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;

public class WordTableCreateUtil {
	
	private static long cellWidth=900;
	
	public static XWPFTable ctreateBaseTableWithBd(XWPFDocument docWord,int rowNum,int colNum){
		XWPFTable table = docWord.createTable();
		CTTbl ttbl = table.getCTTbl();
		CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
		CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
		CTJc cTJc=tblPr.addNewJc();
		cTJc.setVal(STJc.CENTER);
		tblWidth.setW(new BigInteger(String.valueOf(cellWidth*colNum)));
		tblWidth.setType(STTblWidth.DXA);
		return table;
	}
	
	public static void ctreateCell(XWPFTableCell cell,int width,String bgcolor,String text){
		ctreateCell(cell, width, bgcolor, text, false);
	}
	
	public static void ctreateCell(XWPFTableCell cell,int width,String bgcolor,String text,boolean span){
		CTTc cttc = cell.getCTTc();
		CTTcPr cellPr = cttc.addNewTcPr();
		
		if(0==width){
			cellPr.addNewTcW().setW(BigInteger.valueOf(cellWidth));
		}else{
			cellPr.addNewTcW().setW(BigInteger.valueOf(width));
		}
		
		if(StringUtils.isNotEmpty(bgcolor)){
			cell.setColor(bgcolor);
		}
		if(span){
			cellPr.addNewHMerge().setVal(STMerge.RESTART);
		}else{
			cellPr.addNewHMerge().setVal(null);
		}
		cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);
		cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
		cttc.getPList().get(0).addNewR().addNewT().setStringValue(text);
	}
	
	
	
}
